﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace delegeteandevent
{



    class eventbuy
    {
        //定义委托
        public delegate void BuyEventHandler();
        //定义事件对象
        public event BuyEventHandler OnBuy;

        //时间触发器
        public void Issue()
        {
            if(OnBuy !=null)
            {
                Console.WriteLine("水果到了");
                //触发事件
                OnBuy();
            }
        }

    }
    //时间的订阅者
    class MrZhang
    {
        public void Receive()
        {
            Console.WriteLine("张三收到消息，要买苹果");
        }
        //注册时事件
        public  void Register(eventbuy e)
        {
            e.OnBuy += Receive;
            e.OnBuy -= Receive;
            //e,OnBuy=Receive;//时间不允许直接赋值操作
        }
    }
    class Mrwang
    {
        public void Receive()
        {
            Console.WriteLine("王五收到消息，要买橘子");
        }
        //注册时间
        public void Register(eventbuy e)
        {
            e.OnBuy += Receive;
        }
    }
}
